﻿@using IOA.MES.DataAccess.VModel
@{
    ViewBag.Title = "分片";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var orderProduct = (VOrderProduct)ViewBag.OrderProduct;
    var machines = (List<VMachine>)ViewBag.Machines;
    var colors = (List<VColorSet>)ViewBag.Colors;

    var eletricProduct = ViewBag.EletricProduct as VAllocateProduct;

    var cellAreas = ViewBag.CellArea as List<VCellPara>;
    var cellEfficiencies = ViewBag.CellEfficiency as List<VCellPara>;
}
@model IOA.MES.DataAccess.VModel.VWorkOrder
@section Contents{
    <style>
        .layui-col-xs3 > div {
            line-height: 30px;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid layui-form">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-row">
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.WorkorderNo}}：</label>
                        <div class="layui-input-inline">
                            @Model.WorkOrderNo
                        </div>
                    </div>
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.OrderNo}}：</label>
                        <div class="layui-input-inline">
                            @Model.OrderNo
                        </div>
                    </div>
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.WorkShop}}：</label>
                        <div class="layui-input-inline">
                            @Model.WorkshopName
                        </div>
                    </div>
                    <div class="layui-col-xs3 layui-inline">
                        <label class="layui-form-label">{{lang.Field.Count}}：</label>
                        <div class="layui-input-inline">
                            @Model.Count
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" style="margin:0;">
                    <label class="layui-form-label">{{lang.Field.ComponentProductCode}}：</label>
                    <div class="layui-input-block">
                        @(Model == null ? string.Empty : Model.ProductCode)
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">{{lang.Field.ComponentDesc}}：</label>
                    <div class="layui-input-block">
                        @(Model == null ? string.Empty : Model.ProductName)
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-card-header">{{lang.Field.ComponentParams}}</div>
            <div class="layui-card-body">
                <div class="layui-row">
                    <div class="layui-col-xs3">
                        <div>{{lang.Field.CrystalCategory}}：</div>
                    </div>
                    <div class="layui-col-xs3">
                        <div>{{lang.Field.CellEfficiency}}：@(eletricProduct?.Effectiveness)</div>
                    </div>
                    <div class="layui-col-xs3">
                        <div>{{lang.Field.CellArea}}：</div>
                    </div>
                    <div class="layui-col-xs3">
                        <div>{{lang.Field.CellPower}}：@(eletricProduct?.Power)</div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-xs3">
                        <div>{{lang.Field.CellSpecs}}：@(eletricProduct?.Specifications)</div>
                    </div>
                    <div class="layui-col-xs3">
                        <div>{{lang.Field.CellThickness}}：@(eletricProduct?.Thickness)</div>
                    </div>
                    <div class="layui-col-xs3">
                        <div>{{lang.Field.CellFactory}}：@(eletricProduct?.ProductionCompany)</div>
                    </div>
                    <div class="layui-col-xs3">
                        <div>{{lang.Field.CellLevel}}：</div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-xs3">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="padding-left:0;text-align:left;">{{lang.Field.CellColor}}：</label>
                            <div class="layui-input-inline" style="width:150px;">
                                <select id="selCellColors">
                                    <option value="">请选择</option>
                                    @foreach (var item in colors)
                                    {
                                        <option value="@item.ColorCode">@item.ColorCode</option>
                                    }
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs3">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="padding-left:0;text-align:left;">{{lang.Field.CellArea}}：</label>
                            <div class="layui-input-inline" style="width:150px;">
                                <select id="selCellAreas">
                                    <option value="">请选择</option>
                                    @foreach (var item in cellAreas)
                                    {
                                        var keyValuePair = Newtonsoft.Json.JsonConvert.DeserializeObject<KeyValuePair<string, decimal>>(item.ParaValue);
                                        <option value="@(keyValuePair.Value)">@($"{keyValuePair.Key}-{keyValuePair.Value}")</option>
                                    }
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs3">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="padding-left:0;text-align:left;">{{lang.Field.CellEfficiency}}：</label>
                            <div class="layui-input-inline" style="width:150px;">
                                <select id="selCellEfficiencies">
                                    <option value="">请选择</option>
                                    @foreach (var item in cellEfficiencies)
                                    {
                                        <option value="@item.ParaValue">@item.ParaValue</option>
                                    }
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs3">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="padding-left:0;text-align:left;">{{lang.Field.CellPackNo}}：</label>
                            <div class="layui-input-inline" style="width:150px;">
                                <input type="text" name="CellBoxNo" class="layui-input" placeholder="请输入电池片箱号" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-card-header">{{lang.Field.WeldingMachineList}}</div>
            <div class="layui-card-body">
                @for (int i = 0; i < (machines.Count % 4 == 0 ? machines.Count / 4 : machines.Count / 4 + 1); i++)
                {
                    <div class="layui-row">
                        @for (int j = 0; j < 4 && (4 * i + j) < machines.Count; j++)
                        {
                            <div class="layui-col-xs3">
                                <div>
                                    <input type="radio" name="MachineId" title="@machines[4 * i + j].MachineName" value="@machines[4 * i + j].PkId" />
                                </div>
                            </div>
                        }
                    </div>
                }
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-tab">
                    <ul class="layui-tab-title">
                        <li class="layui-this">{{lang.Field.SingleBarcode}}</li>
                        <li>{{lang.Field.MultipleBarcode}}</li>
                        <li>{{lang.Field.BatchBarcode}}</li>
                    </ul>
                    <div class="layui-tab-content">
                        <input type="hidden" name="WorkOrderNo" value="@Model.WorkOrderNo" />
                        <input type="hidden" name="WorkshopId" value="@Model.WorkshopCode" />
                        <div class="layui-tab-item layui-show">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <div class="layui-input-inline" style="width:400px;">
                                        <input type="text" name="SingleCode" class="layui-input" placeholder="请输入分片条码" />
                                    </div>
                                    <button type="button" lay-submit lay-filter="submit1" class="layui-btn layui-btn-normal">{{lang.Field.DetermineSharding}}</button>
                                </div>
                            </div>
                        </div>
                        <div class="layui-tab-item">
                            <div class="layui-form-item">
                                <div class="layui-block">
                                    <div class="layui-input-block" style="margin-left:0;">
                                        <textarea name="MultipleCode" class="layui-textarea" placeholder="请输入分片条码，可多行输入"></textarea>
                                    </div>
                                    <button type="button" lay-submit lay-filter="submit2" class="layui-btn layui-btn-normal" style="margin-top:10px;">{{lang.Field.DetermineSharding}}</button>
                                </div>
                            </div>
                        </div>
                        <div class="layui-tab-item">
                            <div class="layui-row">
                                <div class="layui-col-md4">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" style="padding-left: 0;text-align: left;">{{lang.Field.BarcodeFront}}：</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="CodeFront" class="layui-input" placeholder="请输入条码前端" />
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md4">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" style="padding-left: 0;text-align: left;">{{lang.Field.BarcodeEnd}}：</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="CodeEnd" class="layui-input" placeholder="请输入条码末尾" />
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md4">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" style="padding-left: 0;text-align: left;">{{lang.Field.NumberOfDigits}}：</label>
                                        <div class="layui-input-inline">
                                            <input type="number" name="SerialNumCount" class="layui-input" placeholder="请输入序号位数" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-row" style="margin-top:20px;">
                                <div class="layui-col-md3">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" style="padding-left: 0;text-align: left;">{{lang.Field.StartSequenceNumber}}：</label>
                                        <div class="layui-input-inline" style="width:150px;">
                                            <input type="number" name="StartNum" class="layui-input startendnum" placeholder="请输入起始序号" />
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md3">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" style="padding-left: 0;text-align: left;">{{lang.Field.EndSequenceNumber}}：</label>
                                        <div class="layui-input-inline" style="width:150px;">
                                            <input type="number" name="EndNum" class="layui-input startendnum" placeholder="请输入截止序号" />
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md3">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" style="padding-left: 0;text-align: left;">{{lang.Field.BarCodeQuantity}}：</label>
                                        <div class="layui-input-inline" style="width:150px;">
                                            <input type="number" name="CodeCount" class="layui-input" placeholder="0" disabled />
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md3">
                                    <div class="layui-inline">
                                        <button type="button" lay-submit lay-filter="submit3" class="layui-btn layui-btn-normal">{{lang.Field.DetermineSharding}}</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-header">{{lang.Field.ShardingRecord}}</div>
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form', 'element'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form;

                var options = {
                    elem: '#table'
                    , url: '/WorkOrder/QueryDistributes'
                    , method: "post"
                    , where: { workOrderNo: "@Model.WorkOrderNo" }
                    , page: true
                    , limit: 1000
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers" }
                        , { field: 'MachineName', title: '机台' }
                        , { field: 'CellBoxNo', title: '电池片箱号' }
                        , { field: 'CellColor', title: '电池片颜色' }
                        , { field: 'CodeCount', title: '分片数量' }
                        , { field: 'CNCreateBy', title: '分片人' }
                        , { field: 'CreateTimeStr', title: '分片时间' }
                        , {
                            title: '查看条码', templet: function (d) {
                                return "<a class='layui-btn layui-btn-xs' onclick='showCodes(\"" + d.PkId + "\")'>查看条码</a>";
                            }
                        }
                    ]]
                };

                queryTable();
                function queryTable() {
                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }

                $(document).on("keyup", ".startendnum", function () {
                    var startNum = $("input[name=StartNum]").val() == "" ? null : parseInt($("input[name=StartNum]").val().trim()),
                        endNum = $("input[name=EndNum]").val() == "" ? null : parseInt($("input[name=EndNum]").val().trim());
                    $("input[name=CodeCount]").val((startNum == null || endNum == "" || startNum > endNum) ? "" : endNum - startNum + 1);
                });

                form.on('submit(submit1)', function (data) {
                    var field = data.field;
                    if (field.SingleCode.trim() == '') {
                        layer.msg("请填写条码", { icon: 5 });
                        return;
                    }
                    data.field.DistributeType = 0;
                    submit(field);
                    return false;
                });
                form.on('submit(submit2)', function (data) {
                    var codes = data.field.MultipleCode.trim();
                    if (codes == '') {
                        layer.msg("请至少填写1个条码", { icon: 5 });
                        return;
                    }

                    data.field.DistributeType = 1;
                    submit(data.field, codes.split('\n'));
                    return false;
                });
                form.on('submit(submit3)', function (data) {
                    var field = data.field;
                    if (field.SerialNumCount.trim() == '') {
                        layer.msg("请填写序号位数", { icon: 5 });
                        return;
                    } else if (field.StartNum.trim() == '') {
                        layer.msg("请填写起始序号", { icon: 5 });
                        return;
                    } else if (field.EndNum.trim() == '') {
                        layer.msg("请填写截止序号", { icon: 5 });
                        return;
                    }

                    data.field.DistributeType = 2;
                    submit(data.field);
                    return false;
                });
                function submit(data, codes) {
                    var cellColor = $("#selCellColors").val().trim(),
                        cellEfficiency = $("#selCellEfficiencies").val().trim() == "" ? 0 : parseFloat($("#selCellEfficiencies").val().trim()),
                        cellArea = $("#selCellAreas").val().trim() == "" ? 0 : parseFloat($("#selCellAreas").val().trim());
                    data.CellColor = cellColor;
                    data.CellEfficiency = cellEfficiency;
                    data.CellArea = cellArea;

                    if (cellColor == "") {
                        layer.msg("请选择电池片颜色<br/>Vui lòng chọn màu pin", { icon: 5 });
                        return;
                    } else if (data.CellArea <= 0) {
                        layer.msg("请选择电池片面积<br/>Vui lòng chọn khu vực di động", { icon: 5 });
                        return;
                    } else if (data.CellEfficiency <= 0) {
                        layer.msg("请选择电池片效率<br/>Vui lòng chọn hiệu quả di động", { icon: 5 });
                        return;
                    } else if (!data.MachineId || data.MachineId == '') {
                        layer.msg("请选择焊接机<br/>Vui lòng chọn máy hàn", { icon: 5 });
                        return;
                    }
                    console.log(data);
                    var loadId = layer.load();
                    $.ajax({
                        url: "/WorkOrder/Distribute",
                        data: {
                            model: data,
                            codes: codes ? codes : ""
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            layer.msg("分片成功！");
                            queryTable();
                        }
                    });
                }
            });
        }

        function showCodes(taskId) {
            parent.parent.layui.index.openTabsPage("/WorkOrder/DistributeCodes?distributeTaskId=" + taskId, "分片条码");
        }
    </script>
}
